package com.lcy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcy.entity.Book;
import com.lcy.mapper.BookMapper;
import com.lcy.service.BookService;
import com.lcy.vo.BarVO;
import com.lcy.vo.PieVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService service;
    @Autowired
    private BookMapper mapper;

    @GetMapping("/findAll")
    public List<Book> findAll(){
        return  service.list();
    }

    @GetMapping("/find/{id}")
    public Book findById(@PathVariable("id") Integer id )
    {
        return  mapper.selectById(id);
    }
    @RequestMapping("/find/{page}/{limit}")
    public List<Book> findByPage(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit){
        return  service.findByPage(page, limit);
    }
    @PostMapping("/add")
    public Integer  add(@RequestBody Book book){
        return mapper.insert(book);
    }

    @PutMapping("/update")
    public Integer update(@RequestBody Book book){
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("id",book.getId());
        return mapper.update(book,wrapper);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id){
         mapper.deleteById(id);
    }

    @GetMapping("/pie")
    public List<PieVO> pieVO(){
        return service.pie();
    }
    @GetMapping("/bar")
    public BarVO barVO(){
        return  service.bar();
    }
}

